/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.jcwde.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipsejcde.core.JCDECore;
import org.eclipsejcde.jcwde.dialogs.JCAppletSelectionDialog;
import org.eclipsejcde.jcwde.options.OptionsPreparer;

/**
 * @author Amgad S Youssef
 *
 */
public class DebugJCWDE implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;

	@Override
	public void run(IAction action) {
		try {
			JCAppletSelectionDialog dialog = new JCAppletSelectionDialog(window.getShell());
			int response = dialog.open();
			if (response == Window.OK) {
				ICompilationUnit[] compilationUnits = dialog.getSelectedJCApplets();
				IStatus status = JCDECore.startJCWDE(compilationUnits, OptionsPreparer.prepareJCWDEOptions(), true);
				if (status.getSeverity() != IStatus.OK) {
					MessageDialog.openError(window.getShell(), "Failed to start JCWDE!", status.getMessage());
				}
			}
		} catch (Exception e) {
			MessageDialog.openError(window.getShell(), "Error", e.getMessage());
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}

}
